<?php
require_once( dirname(dirname(__FILE__)) . '/wp-load.php' );
	if($_POST){
		switch($_POST['auto_action']){
			case "Add_new":
			
				 if($_FILES['litpic']['tmp_name']){
					 $up=new upphoto;
					  $up->get_ph_tmpname($_FILES['litpic']['tmp_name']);
					  $up->get_ph_type($_FILES['litpic']['type']);
					  $up->get_ph_size($_FILES['litpic']['size']);
					  $up->get_ph_name($_FILES['litpic']['name']);
					  $img = $up->save();
				 }
				global $wpdb;
				$cat_type = $wpdb->get_results("SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = ".$_POST['typeid']);
				$cat_type = $cat_type[0]->taxonomy;
				switch($cat_type){
					case "cgvideoset":
						$post_type = "cgvideo";
						break;
					case "animationcat":
						$post_type = "animation";
						break;
					case "musicscat":
						$post_type = "music";
						break;
					case "gamescat":
						$post_type = "games";
						break;
					case "recreationscat":
						$post_type = "recreation";
						break;
					case "advisorycat":
						$post_type = "advisory";
						break;
		
				}
				$current_user = wp_get_current_user();
		
				$post = array(
					'ID' => '', //需要更新的文章编号
					'post_author' => $current_user->ID, //作者编号
					'post_content' =>$_POST['body'], //文章内容，必填
					'post_excerpt' => $_POST['description'], //摘要信息
					'post_name' => $_POST['subtitle'], // (slug) 文章别名
					'post_status' => 'pending', //新文章的状态。
					'post_title' => $_POST['title'], //文章标题，必填
					'post_type' => $post_type, //文章类型：文章、页面、链接、菜单、其他定制类型
					'tags_input' => $_POST['tags'] //标签字符
				);
				
				$id = wp_insert_post( $post );
				wp_set_post_terms( $id, $_POST['typeid'], $cat_type );
				add_post_meta($id, 'sub_title', $_POST['subtitle'], true);
				if($img){
					add_post_meta($id, 'show_image', $img, true);
				}
				//header("Location: ".home_url()."/member/#new&act=ok"); 
				echo "<script type=text/javascript>"; 
				echo "window.location.href='".home_url()."/member/#new&act=ok'";   
				echo "</script>"; 
				break;
				
			case "edit_post":
				 if($_FILES['litpic']['tmp_name']){
					 $up=new upphoto;
					  $up->get_ph_tmpname($_FILES['litpic']['tmp_name']);
					  $up->get_ph_type($_FILES['litpic']['type']);
					  $up->get_ph_size($_FILES['litpic']['size']);
					  $up->get_ph_name($_FILES['litpic']['name']);
					  $img = $up->save();
				 }
				global $wpdb;
				$cat_type = $wpdb->get_results("SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = ".$_POST['typeid']);
				$cat_type = $cat_type[0]->taxonomy;
				switch($cat_type){
					case "cgvideoset":
						$post_type = "cgvideo";
						break;
					case "animationcat":
						$post_type = "animation";
						break;
					case "musicscat":
						$post_type = "music";
						break;
					case "gamescat":
						$post_type = "games";
						break;
					case "recreationscat":
						$post_type = "recreation";
						break;
					case "advisorycat":
						$post_type = "advisory";
						break;
		
				}
				$current_user = wp_get_current_user();
			if($post_type == $_POST['post_type']){
				$post = array(
					'ID' => $_POST['post_id'], //需要更新的文章编号
					'post_author' => $current_user->ID, //作者编号
					'post_content' =>$_POST['body'], //文章内容，必填
					'post_excerpt' => $_POST['description'], //摘要信息
					'post_name' => $_POST['subtitle'], // (slug) 文章别名
					'post_status' => 'pending', //新文章的状态。
					'post_title' => $_POST['title'], //文章标题，必填
					'post_type' => $post_type, //文章类型：文章、页面、链接、菜单、其他定制类型
					'tags_input' => $_POST['tags'] //标签字符
				);	
				$id = wp_insert_post( $post );
				wp_set_post_terms( $id, $_POST['typeid'], $cat_type );
				update_post_meta($id, 'sub_title', $_POST['subtitle']);
				if($img){
					update_post_meta($id, 'show_image', $img);
				}
			}else{
				wp_delete_post($_POST['post_id']);
				$post = array(
					'ID' => '', //需要更新的文章编号
					'post_author' => $current_user->ID, //作者编号
					'post_content' =>$_POST['body'], //文章内容，必填
					'post_excerpt' => $_POST['description'], //摘要信息
					'post_name' => $_POST['subtitle'], // (slug) 文章别名
					'post_status' => 'pending', //新文章的状态。
					'post_title' => $_POST['title'], //文章标题，必填
					'post_type' => $post_type, //文章类型：文章、页面、链接、菜单、其他定制类型
					'tags_input' => $_POST['tags'] //标签字符
				);	
				$id = wp_insert_post( $post );
				wp_set_post_terms( $id, $_POST['typeid'], $cat_type );
				add_post_meta($id, 'sub_title', $_POST['subtitle'], true);
				if($img){
					add_post_meta($id, 'show_image', $img, true);
				}
			}
				$url = home_url()."/member/#video&act=edit&aid=".$id; 
				echo "<script type=text/javascript>"; 
				echo 'alert("编辑成功");';  
				echo "window.location.href='".$url."'";    
				echo "</script>"; 
				break;
				
			case "delstow":
				global $table_prefix, $wpdb;
				$userid = $current_user->ID;
				$table_name = $table_prefix."collect";
				foreach($_POST['aids'] as $v){
					$data = $wpdb->get_results("DELETE FROM $table_name WHERE  c_postid = ".$v." and c_userid = ".$userid);	
				}
				$url = home_url()."/member/#fav"; 
				echo "<script type=text/javascript>"; 
				echo 'alert("删除成功");';  
				echo "window.location.href='".$url."'";   
				echo "</script>"; 
				break;
				
			case "Add_specialt":
			
/*				 if($_FILES['img']['tmp_name']){
					 $up=new upphoto;
					  $up->get_ph_tmpname($_FILES['img']['tmp_name']);
					  $up->get_ph_type($_FILES['img']['type']);
					  $up->get_ph_size($_FILES['img']['size']);
					  $up->get_ph_name($_FILES['img']['name']);
					  $img = $up->save();
				 }*/
				$current_user = wp_get_current_user();
				if($current_user->roles[0] == "administrator"){
						$status = "publish";
					}else{
						$status = "pending";
					}

				$post = array(
					'ID' => '', //需要更新的文章编号
					'post_author' => $current_user->ID, //作者编号
					'post_content' =>$_POST['title'], //文章内容，必填
					'post_excerpt' => $_POST['description'], //摘要信息
					'post_status' => $status, //新文章的状态。
					'post_title' => $_POST['title'], //文章标题，必填
					'post_type' => "specialt", //文章类型：文章、页面、链接、菜单、其他定制类型
					'tags_input' => $_POST['tags'] //标签字符
				);
				
				$id = wp_insert_post( $post );
				
				 //$filename = $_FILES['img']['name'];
				 
				include_once ABSPATH . 'wp-admin/includes/media.php';
				include_once ABSPATH . 'wp-admin/includes/file.php';
				include_once ABSPATH . 'wp-admin/includes/image.php';
				$file = $_FILES['img'];
				$upload = wp_handle_upload($file, array('test_form' => false));
				$wp_filetype = wp_check_filetype(basename($upload['file']), null );
				$title      = $file['name'];
				$ext        = strrchr($title, '.');
				$title      = ($ext !== false) ? substr($title, 0, -strlen($ext)) : $title;
				$attachment = array(
				 'post_mime_type' => $wp_filetype['type'],
				 'post_title' => addslashes($title),
				 'post_content' => '',
				 'post_status' => 'inherit'
				);
				$attach_id = wp_insert_attachment($attachment,$upload['file'],$id);


/*				 $wp_filetype = wp_check_filetype(basename($filename), null );
				 $wp_upload_dir = wp_upload_dir();
				 echo $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename );exit;
				 $attachment = array(
				 	 guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename ),
				 	 post_type' => 'attachment',
					 post_mime_type' => $wp_filetype['type'],
					 post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
					 post_content' => '',
					 post_status' => 'inherit'
				  );
				$attach_id = wp_insert_attachment($attachment, $filename, $id );*/ 
				$attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
				wp_update_attachment_metadata( $attach_id,  $attach_data ); 
				
				//$image = media_sideload_image($img,$id,$_POST['title']);
				
				add_post_meta($id, '_thumbnail_id', $attach_id, true);
				
				if($_POST['is_bangumi'] == 1){
					wp_set_post_terms( $id, $_POST['styleid'], "specialtcat" );
					add_post_meta($id, 'sp_alias', $_POST['sp_alias'], true);
					add_post_meta($id, 'person', $_POST['person'], true);
					add_post_meta($id, 'areaid', $_POST['areaid'], true);
					add_post_meta($id, 'typeid', $_POST['typeid'], true);
				}
				echo "<script type=text/javascript>"; 
				echo "window.location.href='".home_url()."/member/#sp&act=ok'";   
				echo "</script>"; 				

				break;
			case "edit_specialt":
				$current_user = wp_get_current_user();
				if($current_user->roles[0] == "administrator"){
						$status = "publish";
					}else{
						$status = "pending";
					}
				$postid = $_POST['spid'];
				$post = array(
					'ID' => $postid, //需要更新的文章编号
					'post_author' => $current_user->ID, //作者编号
					'post_content' =>$_POST['title'], //文章内容，必填
					'post_excerpt' => $_POST['description'], //摘要信息
					'post_status' => $status, //新文章的状态。
					'post_title' => $_POST['title'], //文章标题，必填
					'post_type' => "specialt", //文章类型：文章、页面、链接、菜单、其他定制类型
					'tags_input' => $_POST['tags'] //标签字符
				);
				
				$id = wp_insert_post( $post );
				
				if($_FILES['img']){					 
					include_once ABSPATH . 'wp-admin/includes/media.php';
					include_once ABSPATH . 'wp-admin/includes/file.php';
					include_once ABSPATH . 'wp-admin/includes/image.php';
					$file = $_FILES['img'];
					$upload = wp_handle_upload($file, array('test_form' => false));
					$wp_filetype = wp_check_filetype(basename($upload['file']), null );
					$title      = $file['name'];
					$ext        = strrchr($title, '.');
					$title      = ($ext !== false) ? substr($title, 0, -strlen($ext)) : $title;
					$attachment = array(
					 'post_mime_type' => $wp_filetype['type'],
					 'post_title' => addslashes($title),
					 'post_content' => '',
					 'post_status' => 'inherit'
					);
					
					$attach_id = wp_insert_attachment($attachment,$upload['file'],$id);
					$attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
					wp_update_attachment_metadata( $attach_id,  $attach_data ); 
					
					
					update_post_meta($id, '_thumbnail_id', $attach_id);
				}
				
				if($_POST['is_bangumi'] == 1){
					update_post_meta( $id, $_POST['styleid'], "specialtcat" );
					update_post_meta($id, 'sp_alias', $_POST['sp_alias']);
					update_post_meta($id, 'person', $_POST['person']);
					update_post_meta($id, 'areaid', $_POST['areaid']);
					update_post_meta($id, 'typeid', $_POST['typeid']);
				}
				
				$url = home_url()."/member/#sp&act=edit&spid=".$id; 
				echo "<script type=text/javascript>"; 
				echo 'alert("编辑成功");';  
				echo "window.location.href='".$url."'";    
				echo "</script>"; 
			
				break;
	  }
	}elseif($_GET){
		switch($_GET['act']){
			case "del":
				global $table_prefix, $wpdb;
				$userid = $current_user->ID;
				$table_name = $table_prefix."collect";
				$data = $wpdb->get_results("DELETE FROM $table_name WHERE  c_postid = ".$_GET['aid']." and c_userid = ".$userid);	
				$url = home_url()."/member/#fav"; 
				echo "<script type=text/javascript>"; 
				echo 'alert("删除成功");';  
				echo "window.location.href='".$url."'";   
				echo "</script>"; 
			break;
		}
	}
?>